package yannan.voiplock4ble.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;

import java.util.List;

import yannan.voiplock4ble.BuildConfig;
import yannan.voiplock4ble.R;
import yannan.voiplock4ble.adapter.base.MyViewHolder;
import yannan.voiplock4ble.adapter.base.RecyclerViewBaseAdapter;
import yannan.voiplock4ble.bean.LockKeyBean;
import yannan.voiplock4ble.bean.PwdKeyBean;
import yannan.voiplock4ble.bean.VeinKeyBean;
import yannan.voiplock4ble.view.RecyclerSupportViewEmpty;

/**
 * 说明：指静脉钥匙列表
 * 作者：mainTel
 * 时间：2016/8/25 15:03
 * 备注：
 */
public class LockVeinKeyAdapter extends RecyclerViewBaseAdapter<LockKeyBean> {


    MyViewHolder lastHolder;

    RecyclerSupportViewEmpty recyclerView;

    int lastPoi = -1;

    @Override
    public void clear() {
        super.clear();
        lastHolder = null;
    }

    public LockVeinKeyAdapter(List<LockKeyBean> list, Context ctx, RecyclerView recyclerView) {
        super(list, ctx);
        this.recyclerView = (RecyclerSupportViewEmpty) recyclerView;
        this.recyclerView.setOnMenuOpenListener(iOnMenuOpenListener);
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_vein_key;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int poi) {
        LockKeyBean bean = list.get(poi);
        holder.get(R.id.ll_test).setVisibility(View.GONE);
        holder.setTextView(R.id.tv_number, bean.userIdentifier);
        if (bean.getNickName() != null) {
            holder.setTextView(R.id.tv_name, bean.nickName);
        } else {
            holder.setTextView(R.id.tv_name, "");
        }
        if (bean.userPermission.equals("0")) {
            holder.get(R.id.iv_iaAdmin).setVisibility(View.GONE);
            holder.get(R.id.iv_admin).setBackgroundResource(R.drawable.ic_add_admin);
        } else {
            holder.get(R.id.iv_iaAdmin).setVisibility(View.VISIBLE);
            holder.get(R.id.iv_admin).setBackgroundResource(R.drawable.ic_del_admin);
        }
        if (bean.isBan.equals("0")) {
            holder.get(R.id.iv_is_ban).setVisibility(View.VISIBLE);
            holder.get(R.id.iv_ban).setBackgroundResource(R.drawable.ic_not_disabled);
        } else {
            holder.get(R.id.iv_is_ban).setVisibility(View.GONE);
            holder.get(R.id.iv_ban).setBackgroundResource(R.drawable.ic_key_ban);
        }
        holder.setTextView(R.id.tv_key_time, bean.registerTime.split(" ")[0]);
        if (poi == 0) {
            holder.get(R.id.line_top).setVisibility(View.GONE);
        } else {
            holder.get(R.id.line_top).setVisibility(View.VISIBLE);
        }
        holder.get(R.id.rl_key).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastHolder != null && lastHolder != holder)
                    lastHolder.get(R.id.ll_test).setVisibility(View.GONE);
                holder.get(R.id.ll_test).setVisibility(holder.get(R.id.ll_test).getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                lastHolder = holder;
                lastPoi = poi;
            }
        });
        holder.get(R.id.rl_del).setOnClickListener(new View.OnClickListener() { //删除
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, poi);
            }
        });
        holder.get(R.id.rl_remark).setOnClickListener(new View.OnClickListener() {//时效设置
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, poi);
            }
        });
        holder.get(R.id.rl_ban).setOnClickListener(new View.OnClickListener() { //启用禁用
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, poi);
            }
        });

        holder.get(R.id.rl_admin).setOnClickListener(new View.OnClickListener() { //启用禁用
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v, poi);
            }
        });
    }

    RecyclerSupportViewEmpty.IOnMenuOpenListener iOnMenuOpenListener = new RecyclerSupportViewEmpty.IOnMenuOpenListener() {
        @Override
        public void onMenuOpen(LinearLayoutManager linearLayoutManager, int i) {
            if (lastHolder != null && lastHolder.get(R.id.ll_test).getVisibility() == View.VISIBLE && lastPoi == i) {
                recyclerView.smoothScrollBy(0, 100);
            }
        }
    };
}
